{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "(442, 1)\nfirst: (422, 1)\nsecond: (422, 2)\nMean Squared Error: 2548.0723987259707\n"
          ],
          "output_type": "stream"
        },
        {
          "data": {
            "text/plain": "\u003cFigure size 432x288 with 1 Axes\u003e",
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHUNJREFUeJzt3X2UXHWd5/H3t0N46IBCTIghD1VxCCxRnAgtxBkWeZiswhyNjDDg6eHBZWw9wp7xDMcD2usDe+wDoqODw4HZRlgTUoASRmUV9wywILhHwU5CIiELCaE76SQkIaASmk1M+rt/3FvpolMPt6qr6lbd+3mdU6erb/2q+ts3VZ/8+vf73XvN3RERkeTqiLsAERFpLAW9iEjCKehFRBJOQS8iknAKehGRhFPQi4gknIJeRCThFPQiIgmnoBcRSbjD4i4AYNq0aZ7NZuMuQ0SkraxcufJVd59eqV1LBH02m2VgYCDuMkRE2oqZDUVpp6EbEZGEU9CLiCScgl5EJOEU9CIiCaegFxFJOAW9iEiNcrkc2WyWjo4OstksuVwu7pKKaonllSIi7SaXy9HT08PIyAgAQ0ND9PT0ANDd3R1naYdQj15EpAa9vb0HQz5vZGSE3t7emCoqTUEvIlKDzZs3V7U9Tgp6EZEazJ07t6rtcVLQi4jUoK+vj87Ozrdt6+zspK+vL6aKSlPQi4jUoLu7m/7+fjKZDGZGJpOhv7+/5SZiAczdyzcwOxJ4EjiCYJXOCnf/mpnNA+4HpgKrgMvdfZ+ZHQEsA04HdgOXuvtguZ/R1dXlOqmZiEh1zGylu3dVahelR78XOM/d/xxYC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/j1DTO7l+DPxGXhc+YAw2HAvhN4La46Q/3ABnf/54KfWfX+LPFzC/8SmA1sK9Fm/P4o99xKr9nMOjGz2cCPgSvc/aX8E8q8B5peZ/iX796wnpVm9hJwUti+cLgu9v0ZuoxxvfmY9me5554z7rlPUOv+rNfEQ6vcgG/x9snDW4q0mQq8TDDJcVx4f2rB4zcDN457zsyC+xcBv4mrToL/oKeFbSYTjEF+Lvz+Gt4+CfWjOPcn8A2C3lFHvfdnuB82EUym5ie73juuTdH9AbyXt0/GbiKYPKv4mk2u89iw/SeLvGbR90BMdU4HJoX33wNsLXgP/BZYxNjk4YVx1Rl+30EQmO+Je38WtP0Bh07GlvpMVb0/a/4lWvVGMA73GLAh/JrfOV3A9wva/WeCCbiNwKfHvcYm4D+M23YTwWTYGuDx8Y83s05gCsGKoLVhTbcWfMiOBB4I2z9T+GaOoc7ZBH9WrgeeDW9/X8/9CVwIvEiwuqE33PbfgI9X2h8EQ1MvAS9QsHKh2GvW4X1ZU53AfwXeLNh/zxLMk5R8D8RU5ycL/j1XAR8reM0u4LnwNW8jPFAzjjrDx85hXMcixv35QYL/dN4EdgPryn2mat2fOjJWRCThkrjqRkRECijoRUQSTkEvIpJwCnoRkYRT0IuIJJyCXkQk4RT0IiIJp6AXEUm4/w/uze3A0WX/5QAAAABJRU5ErkJggg\u003d\u003d\n"
          },
          "metadata": {
            "needs_background": "light"
          },
          "output_type": "display_data"
        }
      ],
      "source": "# coding: utf-8\n\n# get_ipython().run_line_magic(\u0027matplotlib\u0027, \u0027inline\u0027)\n%matplotlib inline\n\nimport matplotlib.pylab as plt\nimport numpy as np\nfrom sklearn import datasets\n\nclass LinearRegression():\n    def __init__(self):\n        self.w \u003d None\n        \n    def fit(self, X, y):\n        # Insert constant ones for bias weights\n        print(\"first:\", X.shape)\n        # 在第0项，插入 1，让x0项为1\n        X \u003d np.insert(X, 0, 1, axis\u003d1)\n        print(\"second:\", X.shape)\n        # inv(): 对当前值取逆， dot()：矩阵计算\n        X_ \u003d np.linalg.inv(X.T.dot(X))\n        # 算出来 最好的 一组参数 theta\n        self.w \u003d X_.dot(X.T).dot(y)\n    \n    def predict(self, X):\n        # Insert constant ones for bias weights\n        X \u003d np.insert(X, 0, 1, axis\u003d1)\n        y_pred \u003d X.dot(self.w)\n        return y_pred\n\n    \ndef mean_squared_error(y_true, y_pred):\n    mse \u003d np.mean(np.power(y_true - y_pred, 2))\n    return mse\n\n\ndef main():\n    # load the diabetes dataset\n    diabetes \u003d datasets.load_diabetes()\n    \n    # Use only one feature\n    X \u003d diabetes.data[: , np.newaxis, 2]\n    print(X.shape)\n    # split the data into training/testing sets\n    x_train, x_test \u003d X[:-20], X[-20:]\n    \n    # Split the targets into training/testing sets\n    y_train, y_test \u003d diabetes.target[:-20], diabetes.target[-20:]\n    \n    clf \u003d LinearRegression()\n    clf.fit(x_train, y_train)\n    y_pred \u003d clf.predict(x_test)\n    \n    # Print the mean squared error\n    print(\"Mean Squared Error:\", mean_squared_error(y_test, y_pred))\n    \n    # Plot the results\n    plt.scatter(x_test[: , 0], y_test, color\u003d\u0027black\u0027)\n    plt.plot(x_test[:, 0], y_pred, color\u003d\"blue\", linewidth\u003d3)\n    # plt.show()\n\n    \nmain()\n"
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 0.03807591  0.05068012  0.06169621 ... -0.00259226  0.01990842\n",
            "  -0.01764613]\n",
            " [-0.00188202 -0.04464164 -0.05147406 ... -0.03949338 -0.06832974\n",
            "  -0.09220405]\n",
            " [ 0.08529891  0.05068012  0.04445121 ... -0.00259226  0.00286377\n",
            "  -0.02593034]\n",
            " ...\n",
            " [ 0.04170844  0.05068012 -0.01590626 ... -0.01107952 -0.04687948\n",
            "   0.01549073]\n",
            " [-0.04547248 -0.04464164  0.03906215 ...  0.02655962  0.04452837\n",
            "  -0.02593034]\n",
            " [-0.04547248 -0.04464164 -0.0730303  ... -0.03949338 -0.00421986\n",
            "   0.00306441]]\n",
            "[[ 0.06169621]\n",
            " [-0.05147406]\n",
            " [ 0.04445121]\n",
            " [-0.01159501]\n",
            " [-0.03638469]\n",
            " [-0.04069594]\n",
            " [-0.04716281]\n",
            " [-0.00189471]\n",
            " [ 0.06169621]\n",
            " [ 0.03906215]\n",
            " [-0.08380842]\n",
            " [ 0.01750591]\n",
            " [-0.02884001]\n",
            " [-0.00189471]\n",
            " [-0.02560657]\n",
            " [-0.01806189]\n",
            " [ 0.04229559]\n",
            " [ 0.01211685]\n",
            " [-0.0105172 ]\n",
            " [-0.01806189]\n",
            " [-0.05686312]\n",
            " [-0.02237314]\n",
            " [-0.00405033]\n",
            " [ 0.06061839]\n",
            " [ 0.03582872]\n",
            " [-0.01267283]\n",
            " [-0.07734155]\n",
            " [ 0.05954058]\n",
            " [-0.02129532]\n",
            " [-0.00620595]\n",
            " [ 0.04445121]\n",
            " [-0.06548562]\n",
            " [ 0.12528712]\n",
            " [-0.05039625]\n",
            " [-0.06332999]\n",
            " [-0.03099563]\n",
            " [ 0.02289497]\n",
            " [ 0.01103904]\n",
            " [ 0.07139652]\n",
            " [ 0.01427248]\n",
            " [-0.00836158]\n",
            " [-0.06764124]\n",
            " [-0.0105172 ]\n",
            " [-0.02345095]\n",
            " [ 0.06816308]\n",
            " [-0.03530688]\n",
            " [-0.01159501]\n",
            " [-0.0730303 ]\n",
            " [-0.04177375]\n",
            " [ 0.01427248]\n",
            " [-0.00728377]\n",
            " [ 0.0164281 ]\n",
            " [-0.00943939]\n",
            " [-0.01590626]\n",
            " [ 0.0250506 ]\n",
            " [-0.04931844]\n",
            " [ 0.04121778]\n",
            " [-0.06332999]\n",
            " [-0.06440781]\n",
            " [-0.02560657]\n",
            " [-0.00405033]\n",
            " [ 0.00457217]\n",
            " [-0.00728377]\n",
            " [-0.0374625 ]\n",
            " [-0.02560657]\n",
            " [-0.02452876]\n",
            " [-0.01806189]\n",
            " [-0.01482845]\n",
            " [-0.02991782]\n",
            " [-0.046085  ]\n",
            " [-0.06979687]\n",
            " [ 0.03367309]\n",
            " [-0.00405033]\n",
            " [-0.02021751]\n",
            " [ 0.00241654]\n",
            " [-0.03099563]\n",
            " [ 0.02828403]\n",
            " [-0.03638469]\n",
            " [-0.05794093]\n",
            " [-0.0374625 ]\n",
            " [ 0.01211685]\n",
            " [-0.02237314]\n",
            " [-0.03530688]\n",
            " [ 0.00996123]\n",
            " [-0.03961813]\n",
            " [ 0.07139652]\n",
            " [-0.07518593]\n",
            " [-0.00620595]\n",
            " [-0.04069594]\n",
            " [-0.04824063]\n",
            " [-0.02560657]\n",
            " [ 0.0519959 ]\n",
            " [ 0.00457217]\n",
            " [-0.06440781]\n",
            " [-0.01698407]\n",
            " [-0.05794093]\n",
            " [ 0.00996123]\n",
            " [ 0.08864151]\n",
            " [-0.00512814]\n",
            " [-0.06440781]\n",
            " [ 0.01750591]\n",
            " [-0.04500719]\n",
            " [ 0.02828403]\n",
            " [ 0.04121778]\n",
            " [ 0.06492964]\n",
            " [-0.03207344]\n",
            " [-0.07626374]\n",
            " [ 0.04984027]\n",
            " [ 0.04552903]\n",
            " [-0.00943939]\n",
            " [-0.03207344]\n",
            " [ 0.00457217]\n",
            " [ 0.02073935]\n",
            " [ 0.01427248]\n",
            " [ 0.11019775]\n",
            " [ 0.00133873]\n",
            " [ 0.05846277]\n",
            " [-0.02129532]\n",
            " [-0.0105172 ]\n",
            " [-0.04716281]\n",
            " [ 0.00457217]\n",
            " [ 0.01750591]\n",
            " [ 0.08109682]\n",
            " [ 0.0347509 ]\n",
            " [ 0.02397278]\n",
            " [-0.00836158]\n",
            " [-0.06117437]\n",
            " [-0.00189471]\n",
            " [-0.06225218]\n",
            " [ 0.0164281 ]\n",
            " [ 0.09618619]\n",
            " [-0.06979687]\n",
            " [-0.02129532]\n",
            " [-0.05362969]\n",
            " [ 0.0433734 ]\n",
            " [ 0.05630715]\n",
            " [-0.0816528 ]\n",
            " [ 0.04984027]\n",
            " [ 0.11127556]\n",
            " [ 0.06169621]\n",
            " [ 0.01427248]\n",
            " [ 0.04768465]\n",
            " [ 0.01211685]\n",
            " [ 0.00564998]\n",
            " [ 0.04660684]\n",
            " [ 0.12852056]\n",
            " [ 0.05954058]\n",
            " [ 0.09295276]\n",
            " [ 0.01535029]\n",
            " [-0.00512814]\n",
            " [ 0.0703187 ]\n",
            " [-0.00405033]\n",
            " [-0.00081689]\n",
            " [-0.04392938]\n",
            " [ 0.02073935]\n",
            " [ 0.06061839]\n",
            " [-0.0105172 ]\n",
            " [-0.03315126]\n",
            " [-0.06548562]\n",
            " [ 0.0433734 ]\n",
            " [-0.06225218]\n",
            " [ 0.06385183]\n",
            " [ 0.03043966]\n",
            " [ 0.07247433]\n",
            " [-0.0191397 ]\n",
            " [-0.06656343]\n",
            " [-0.06009656]\n",
            " [ 0.06924089]\n",
            " [ 0.05954058]\n",
            " [-0.02668438]\n",
            " [-0.02021751]\n",
            " [-0.046085  ]\n",
            " [ 0.07139652]\n",
            " [-0.07949718]\n",
            " [ 0.00996123]\n",
            " [-0.03854032]\n",
            " [ 0.01966154]\n",
            " [ 0.02720622]\n",
            " [-0.00836158]\n",
            " [-0.01590626]\n",
            " [ 0.00457217]\n",
            " [-0.04285156]\n",
            " [ 0.00564998]\n",
            " [-0.03530688]\n",
            " [ 0.02397278]\n",
            " [-0.01806189]\n",
            " [ 0.04229559]\n",
            " [-0.0547075 ]\n",
            " [-0.00297252]\n",
            " [-0.06656343]\n",
            " [-0.01267283]\n",
            " [-0.04177375]\n",
            " [-0.03099563]\n",
            " [-0.00512814]\n",
            " [-0.05901875]\n",
            " [ 0.0250506 ]\n",
            " [-0.046085  ]\n",
            " [ 0.00349435]\n",
            " [ 0.05415152]\n",
            " [-0.04500719]\n",
            " [-0.05794093]\n",
            " [-0.05578531]\n",
            " [ 0.00133873]\n",
            " [ 0.03043966]\n",
            " [ 0.00672779]\n",
            " [ 0.04660684]\n",
            " [ 0.02612841]\n",
            " [ 0.04552903]\n",
            " [ 0.04013997]\n",
            " [-0.01806189]\n",
            " [ 0.01427248]\n",
            " [ 0.03690653]\n",
            " [ 0.00349435]\n",
            " [-0.07087468]\n",
            " [-0.03315126]\n",
            " [ 0.09403057]\n",
            " [ 0.03582872]\n",
            " [ 0.03151747]\n",
            " [-0.06548562]\n",
            " [-0.04177375]\n",
            " [-0.03961813]\n",
            " [-0.03854032]\n",
            " [-0.02560657]\n",
            " [-0.02345095]\n",
            " [-0.06656343]\n",
            " [ 0.03259528]\n",
            " [-0.046085  ]\n",
            " [-0.02991782]\n",
            " [-0.01267283]\n",
            " [-0.01590626]\n",
            " [ 0.07139652]\n",
            " [-0.03099563]\n",
            " [ 0.00026092]\n",
            " [ 0.03690653]\n",
            " [ 0.03906215]\n",
            " [-0.01482845]\n",
            " [ 0.00672779]\n",
            " [-0.06871905]\n",
            " [-0.00943939]\n",
            " [ 0.01966154]\n",
            " [ 0.07462995]\n",
            " [-0.00836158]\n",
            " [-0.02345095]\n",
            " [-0.046085  ]\n",
            " [ 0.05415152]\n",
            " [-0.03530688]\n",
            " [-0.03207344]\n",
            " [-0.0816528 ]\n",
            " [ 0.04768465]\n",
            " [ 0.06061839]\n",
            " [ 0.05630715]\n",
            " [ 0.09834182]\n",
            " [ 0.05954058]\n",
            " [ 0.03367309]\n",
            " [ 0.05630715]\n",
            " [-0.06548562]\n",
            " [ 0.16085492]\n",
            " [-0.05578531]\n",
            " [-0.02452876]\n",
            " [-0.03638469]\n",
            " [-0.00836158]\n",
            " [-0.04177375]\n",
            " [ 0.12744274]\n",
            " [-0.07734155]\n",
            " [ 0.02828403]\n",
            " [-0.02560657]\n",
            " [-0.06225218]\n",
            " [-0.00081689]\n",
            " [ 0.08864151]\n",
            " [-0.03207344]\n",
            " [ 0.03043966]\n",
            " [ 0.00888341]\n",
            " [ 0.00672779]\n",
            " [-0.02021751]\n",
            " [-0.02452876]\n",
            " [-0.01159501]\n",
            " [ 0.02612841]\n",
            " [-0.05901875]\n",
            " [-0.03638469]\n",
            " [-0.02452876]\n",
            " [ 0.01858372]\n",
            " [-0.0902753 ]\n",
            " [-0.00512814]\n",
            " [-0.05255187]\n",
            " [-0.02237314]\n",
            " [-0.02021751]\n",
            " [-0.0547075 ]\n",
            " [-0.00620595]\n",
            " [-0.01698407]\n",
            " [ 0.05522933]\n",
            " [ 0.07678558]\n",
            " [ 0.01858372]\n",
            " [-0.02237314]\n",
            " [ 0.09295276]\n",
            " [-0.03099563]\n",
            " [ 0.03906215]\n",
            " [-0.06117437]\n",
            " [-0.00836158]\n",
            " [-0.0374625 ]\n",
            " [-0.01375064]\n",
            " [ 0.07355214]\n",
            " [-0.02452876]\n",
            " [ 0.03367309]\n",
            " [ 0.0347509 ]\n",
            " [-0.03854032]\n",
            " [-0.03961813]\n",
            " [-0.00189471]\n",
            " [-0.03099563]\n",
            " [-0.046085  ]\n",
            " [ 0.00133873]\n",
            " [ 0.06492964]\n",
            " [ 0.04013997]\n",
            " [-0.02345095]\n",
            " [ 0.05307371]\n",
            " [ 0.04013997]\n",
            " [-0.02021751]\n",
            " [ 0.01427248]\n",
            " [-0.03422907]\n",
            " [ 0.00672779]\n",
            " [ 0.00457217]\n",
            " [ 0.03043966]\n",
            " [ 0.0519959 ]\n",
            " [ 0.06169621]\n",
            " [-0.00728377]\n",
            " [ 0.00564998]\n",
            " [ 0.05415152]\n",
            " [-0.00836158]\n",
            " [ 0.114509  ]\n",
            " [ 0.06708527]\n",
            " [-0.05578531]\n",
            " [ 0.03043966]\n",
            " [-0.02560657]\n",
            " [ 0.10480869]\n",
            " [-0.00620595]\n",
            " [-0.04716281]\n",
            " [-0.04824063]\n",
            " [ 0.08540807]\n",
            " [-0.01267283]\n",
            " [-0.03315126]\n",
            " [-0.00728377]\n",
            " [-0.01375064]\n",
            " [ 0.05954058]\n",
            " [ 0.02181716]\n",
            " [ 0.01858372]\n",
            " [-0.01159501]\n",
            " [-0.00297252]\n",
            " [ 0.01750591]\n",
            " [-0.02991782]\n",
            " [-0.02021751]\n",
            " [-0.05794093]\n",
            " [ 0.06061839]\n",
            " [-0.04069594]\n",
            " [-0.07195249]\n",
            " [-0.05578531]\n",
            " [ 0.04552903]\n",
            " [-0.00943939]\n",
            " [-0.03315126]\n",
            " [ 0.04984027]\n",
            " [-0.08488624]\n",
            " [ 0.00564998]\n",
            " [ 0.02073935]\n",
            " [-0.00728377]\n",
            " [ 0.10480869]\n",
            " [-0.02452876]\n",
            " [-0.00620595]\n",
            " [-0.03854032]\n",
            " [ 0.13714305]\n",
            " [ 0.17055523]\n",
            " [ 0.00241654]\n",
            " [ 0.03798434]\n",
            " [-0.05794093]\n",
            " [-0.00943939]\n",
            " [-0.02345095]\n",
            " [-0.0105172 ]\n",
            " [-0.03422907]\n",
            " [-0.00297252]\n",
            " [ 0.06816308]\n",
            " [ 0.00996123]\n",
            " [ 0.00241654]\n",
            " [-0.03854032]\n",
            " [ 0.02612841]\n",
            " [-0.08919748]\n",
            " [ 0.06061839]\n",
            " [-0.02884001]\n",
            " [-0.02991782]\n",
            " [-0.0191397 ]\n",
            " [-0.04069594]\n",
            " [ 0.01535029]\n",
            " [-0.02452876]\n",
            " [ 0.00133873]\n",
            " [ 0.06924089]\n",
            " [-0.06979687]\n",
            " [-0.02991782]\n",
            " [-0.046085  ]\n",
            " [ 0.01858372]\n",
            " [ 0.00133873]\n",
            " [-0.03099563]\n",
            " [-0.00405033]\n",
            " [ 0.01535029]\n",
            " [ 0.02289497]\n",
            " [ 0.04552903]\n",
            " [-0.04500719]\n",
            " [-0.03315126]\n",
            " [ 0.097264  ]\n",
            " [ 0.05415152]\n",
            " [ 0.12313149]\n",
            " [-0.08057499]\n",
            " [ 0.09295276]\n",
            " [-0.05039625]\n",
            " [-0.01159501]\n",
            " [-0.0277622 ]\n",
            " [ 0.05846277]\n",
            " [ 0.08540807]\n",
            " [-0.00081689]\n",
            " [ 0.00672779]\n",
            " [ 0.00888341]\n",
            " [ 0.08001901]\n",
            " [ 0.07139652]\n",
            " [-0.02452876]\n",
            " [-0.0547075 ]\n",
            " [-0.03638469]\n",
            " [ 0.0164281 ]\n",
            " [ 0.07786339]\n",
            " [-0.03961813]\n",
            " [ 0.01103904]\n",
            " [-0.04069594]\n",
            " [-0.03422907]\n",
            " [ 0.00564998]\n",
            " [ 0.08864151]\n",
            " [-0.03315126]\n",
            " [-0.05686312]\n",
            " [-0.03099563]\n",
            " [ 0.05522933]\n",
            " [-0.06009656]\n",
            " [ 0.00133873]\n",
            " [-0.02345095]\n",
            " [-0.07410811]\n",
            " [ 0.01966154]\n",
            " [-0.01590626]\n",
            " [-0.01590626]\n",
            " [ 0.03906215]\n",
            " [-0.0730303 ]]\n"
          ]
        }
      ],
      "source": [
        "diabetes \u003d datasets.load_diabetes()\n",
        "print(diabetes.data)\n",
        "X \u003d diabetes.data[: , np.newaxis, 2]\n",
        "print(X)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}